package com.shuodao.cokeps.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.shuodao.common.util.DateTimeUtil;

public class StatisticsUtils {
	
	public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
	    ArrayList<String> result = new ArrayList<String>();
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
	    Calendar min = Calendar.getInstance();
	    Calendar max = Calendar.getInstance();
	    min.setTime(sdf.parse(minDate));
	    min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
	    max.setTime(sdf.parse(maxDate));
	    max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
	    Calendar curr = min;
	     while (curr.before(max)) {
	      result.add(sdf.format(curr.getTime()));
	      curr.add(Calendar.MONTH, 1);
	     }
	 
	     return result;
	   }
 
 
	public static List<Date> getDateBetween(List<String> list) throws ParseException {
		List<Date> dateList = new ArrayList<Date>();
		for (String date : list) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date1 = sdf.parse(date + "-01");
			dateList.add(date1);
		}
		return dateList;
	}
 /**
  * 
  * @param minDate 开始日期
  * @param maxDate 结束日期
  * @return 日期间隔中 每个日期的起始
  * @throws ParseException
  */
	public static List<Date> getDateByDays(String minDate, String maxDate) throws ParseException{
		List<String> list=getMonthBetween(minDate,maxDate);		
		return getDateBetween(list);
	}
   /**
    * 
    * @param begin 开始时间
    * @param end  结束时间
    * @return  两个日期之间的日期段,以月为分隔 List依次排列 
    */
      
    public static List<Date> getDateInterval(Date begin, Date end) {
		// 开始日期不能大于结束日期
		List<Date> dateList=new ArrayList<Date>();
		if (!begin.before(end)) {
			return null;
		}
		Calendar cal_begin = Calendar.getInstance();
		cal_begin.setTime(begin);
		Calendar cal_end = Calendar.getInstance();
		cal_end.setTime(end);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		while (true) {
			if (cal_begin.get(Calendar.YEAR) == cal_end.get(Calendar.YEAR)
					&& cal_begin.get(Calendar.MONTH) == cal_end
							.get(Calendar.MONTH)) {
				dateList.add(DateTimeUtil.parseDate(sdf.format(cal_begin.getTime())));
				dateList.add(DateTimeUtil.parseDate(sdf.format(cal_end.getTime())));
				break;
			}
			String str_begin = sdf.format(cal_begin.getTime());
			String str_end = getMonthEnd(cal_begin.getTime());
			dateList.add(DateTimeUtil.parseDate(str_begin));
			dateList.add(DateTimeUtil.parseDate(str_end));
			cal_begin.add(Calendar.MONTH, 1);
			cal_begin.set(Calendar.DAY_OF_MONTH, 1);
			// String str_end =;
		}
		return dateList;
	}
    
	public static String getMonthEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		return formatDateByFormat(calendar.getTime(), "yyyy-MM-dd");
	}
	public static String formatDateByFormat(Date date, String format) {
		String result = "";
		if (date != null) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat(format);
				result = sdf.format(date);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return result;
	}
    
	public static void main(String[] args) throws ParseException {
		System.out.println(getMonthBetween("2017-09-25","2017-10-25"));
		System.out.println(getDateBetween(getMonthBetween("2017-09-25","2017-10-25")));
		System.out.println(getDateByDays("2017-09-25","2017-10-25"));
		System.out.println();	
        System.out.println(getDateInterval(DateTimeUtil.parseDate("2016-09-25"), DateTimeUtil.parseDate("2017-10-25")));
	/*	 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Date> list = splitMonth(DateTimeUtil.parseDate("2017-09-25"), DateTimeUtil.parseDate("2017-10-25"), 31);  
        for (Date t : list) {  
            System.out.println(sdf.format(t));  
        } */ 
	}

}
